package com.ykdl.member.kid.marketcard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.MyOrderAdapter;
import com.ykdl.member.kid.beans.OrderBean;
import com.ykdl.member.kid.beans.OrderListBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.kid.widget.RefreshListView.OnRefreshListener;
import com.ykdl.member.kid.xlist.XListView;
import com.ykdl.member.kid.xlist.XListView.IXListViewListener;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

/********************************************************************************
 * 
 ** 作者： jiafeiyu 创始时间：2015-4-27 描述： 我的订单
 *********************************************************************************/
public class MyOrderActivity extends BaseScreen implements IXListViewListener {
	
	private XListView mRefListView;
	private MyOrderAdapter adapter;
	private int cursor = 0;
	private int count = 20;
	private boolean isGoneBt = false;
	private ArrayList<OrderBean> data_list = new ArrayList<OrderBean>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTopTitle("我的订单", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		}, R.string.oreder_help, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, CouponinfoWebView.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra(KidAction.URL_TYPE, 10);
				MyOrderActivity.this.startActivity(intent);
			}
		}, R.drawable.title_bg, false);

		setContent(R.layout.activity_my_order);
		initView();
	}

	private void initView() {
		adapter = new MyOrderAdapter(this);
		mRefListView = (XListView) findViewById(R.id.rl_my_order);
		mRefListView.setPullLoadEnable(true);
		// 允许下拉
		mRefListView.setPullRefreshEnable(true);
		// 设置监听器
		mRefListView.setXListViewListener(this);
		mRefListView.gonefoot(false);
		mRefListView.hideFoot();
		mRefListView.setXListViewListener(this);
		mRefListView.setAdapter(adapter);
		/*mRefListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				OrderBean bean =(OrderBean)adapter.getItem(arg2-1);
				Intent intent=new Intent(MyOrderActivity.this,CouponinfoWebView.class);
				intent.putExtra(KidAction.URL_TYPE, "2");
				intent.putExtra(KidAction.ORDER_ID, bean.getOrder_id()+"");
				MyOrderActivity.this.startActivity(intent);
			}
		});*/
		showProgress();
		getData(true);
		mRefListView.stopRefresh();
		mRefListView.stopLoadMore();
	}

	public void getData(boolean isShowProgress) {
		// TODO 添加分页获取
//		String url = "http://api-hsh-dev.ykbenefit.com/consumer/api/v1/store/order/list/";
		String url = KidConfig.ORDER_LIST;
		Map<String, String> map = new HashMap<String, String>();
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request = Wxxr.getInstance().getRequest(url, map, null);
		TaskManager.startHttpRequest(request, new getDataTag(),
				OrderListBean.class);
	}

	class getDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			finishProgress();
			if (t instanceof OrderListBean) {
				OrderListBean bean = (OrderListBean) t;
				if (bean.getError() == null) {
					if (bean.getOrder_list() == null
							|| bean.getOrder_list().size() == 0) {
						mRefListView.setVisibility(View.GONE);
						showErrorTip("还没有订单,快去门店订购吧", "");
					} else {
						mRefListView.setVisibility(View.VISIBLE);
						if (!isGoneBt) {
							data_list.addAll(bean.getOrder_list());
					}else {
						data_list=bean.getOrder_list();
					}
						adapter.setData(data_list);
						if (bean.getNext_cursor() == bean.getTotal_number()) {
							mRefListView.endLoadMore();
						
							isGoneBt = true;
						} else {
							mRefListView.readyLoadMore();
							cursor = bean.getNext_cursor();
						}
						adapter.notifyDataSetChanged();
						mRefListView.stopRefresh();
						mRefListView.showFoot();
					}
				}
			}

		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			finishProgress();
			mRefListView.stopRefresh();
			mRefListView.endLoadMore();
			return false;
		}

		@Override
		public void refreshCancel() {
		}

	}

	@Override
	public void onRefresh() {
		adapter.clearList();
		cursor=0;
		isGoneBt=true;
		getData(false);
	}

	@Override
	public void onLoadMore() {
		if (isGoneBt) {
			mRefListView.stopLoadMore();
			mRefListView.stopRefresh();
		}else{
			getData(false);
		}
	}

}
